(function($){
    $.fn.extend({
        center: function () {
            return this.each(function() {
                var top = ($(window).height() - $(this).outerHeight()) / 2;
                var left = ($(window).width() - $(this).outerWidth()) / 2;
                $(this).css({position:'absolute', margin:0, top: (top > 0 ? top : 0)+'px', left: (left > 0 ? left : 0)+'px'});
            });
        }
    }); 
})(jQuery);




function change_captcha(){
	$('#captcha').attr('src', "/modules/get_captcha.php?rnd=" + Math.random());
}



/*
	Define some functions shared throughout the entire site!
*/
var Website = {
	
	//Add shadows on the left and right side of the container
	addShadows: function(){
		$('#container').prepend('<div id="lbg"></div>');
		$('#container').prepend('<div id="rbg"></div>');
	},
	
	//Toggle the search visibility
	searchToggle: function(){
		$("#searchClick").click(function () {
			if ($("#search").is(":hidden")) {
				$("#search").slideDown(400);
			}else{
				$("#search").slideUp(400);
			}
		});	
	},
	
	//Cover images hover effect (switch between front and back covers)
	coverHover: function(){
		$('.coverImg').hover(function(){
			$('.imgFront',$(this)).stop(true,true).fadeOut(500,function() {
				$('.imgBack',$(this)).fadeIn(250);
			});
		}, function () {
			//$('.imgBack',$(this)).stop(true,true).fadeTo(200, 1, function(){
				$('.imgFront').fadeIn(500);
			//});
		});
	},
	
	//Button Hover Effect
	buttonHover: function(){
		$(".jBtn").hover(function(){
			$(this).stop(true,true).fadeTo(150,0.7 ,function () {
				$(this).animate({backgroundPosition:"0px bottom"},0,function(){
					$(this).fadeTo(150,1)
				});
			})
		}, function () {
			$(this).stop(true,true).fadeTo(150,0.7 ,function () {
				$(this).animate({backgroundPosition:"0px top"},0,function(){
					$(this).fadeTo(150,1);
				});
			})
		});	
	},
	
	//Append icon, left and right corners to option div (account & payment page)
	optionsIcon: function(){
		$('#options a.option')
			.append('<div class="l"></div>')
			.append('<div class="r"></div>')
			.append('<div class="icon"></div>');	
	}
	
};


$(window).bind('load', function () {
	Website.addShadows();	// Add Shadows To Container
	Website.searchToggle();	// SlideToggle Searchblock
	Website.coverHover();	// Cover Images Hover Effect
	Website.optionsIcon();	// Add Corners + icon to options
	Website.buttonHover();	// Add Hover Effect To Defined Buttons
});




function setCookie(value){
	$.cookie('contentSwitch', value);
}


/* add bookmark script */
function addBookmark(title,url){
	if (window.sidebar) // firefox	
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera	
		var elem = document.createElement('a');	
		elem.setAttribute('href',url);	
		elem.setAttribute('title',title);	
		elem.setAttribute('rel','sidebar');	
		elem.click();
	} 
		else if(document.all)// ie	
		window.external.AddFavorite(url, title);
}
function createModelSearchUrl(text){
	text = text.value;
	temp = text.replace(/[^a-zA-Z 0-9]+/g,'');
	temp = temp.replace(/\s+/g, '-');
	
	var url      = document.location.href;
		url      = url.replace('http://','');
	//Building the url array
	var urlParts = url.split('/');
	var siteUrl  = 'http://'+urlParts[0]+'/'+urlParts[1];
	
	location.href = siteUrl+"/"+urlParts[2]+"/"+temp;
}

function createSearchUrl(text, method){
	
	//Nou deze bagger gaat nog flink uitgebreidt worden
	var text = text.value;

	if(text.length < 3 && text.lenght > 0){
		alert(search4letter);
		return false;
	}


	temp = text.replace(/[^a-zA-Z 0-9]+/g,'');
	temp = temp.replace(/\s+/g, '-');
	var isSearchUrl = false;
	
	//to get the url in chopped parts
	var url      = document.location.href;
	url          = url.replace('http://','');
	//Building the url array
	var urlParts = new Array();
	urlParts     = url.split('/');
	var siteUrl  = urlParts[0]+'/'+urlParts[1];
	for ( var i in urlParts )
	{
		
    	if (urlParts[i] == 'search'){
    		urlParts.splice(i);
    		urlParts.splice(parseInt(i)+1);
    		isSearchUrl = true;
    	}
    	if(urlParts[i] == 'details' || urlParts[i] == 'payment'){

    		urlParts.splice(i);
    		urlParts.splice(parseInt(i)+1);
    		urlParts.splice(parseInt(i)+2);
    	}
    	if(urlParts[i] == ''){
    		urlParts.splice(i);
    	}
    	
	}

	if(method == 'models'){
		location.href = 'http://'+siteUrl+'/models/'+temp+'/';
		return false;
	}
	
	
	if(isSearchUrl){
		if(method == 'all'){
			location.href = 'http://'+siteUrl+'/search/'+temp+'/';
		}else if (method == 'current'){
			//Whe will build the url from the location.href
			loc = "http://";
			for ( var i in urlParts ) {
				if(i==0 || i==1 ){
					loc += urlParts[i]+'/';
				}
			}
			loc += 'search/'+temp+'/';
			location.href = loc;
		}else{
			//This is where the category search steps in
			location.href = 'http://'+siteUrl+'/'+method+'/search/'+temp+'/';
		}
	}else{
		if(method == 'all'){
			location.href = 'http://'+siteUrl+'/search/'+temp+'/';
		}else if(method == 'current'){
			
			loc = "http://";
			for ( var i in urlParts ) {
				if(i==0 || i==1 ){
					loc += urlParts[i]+'/';
				}
			}
			loc += 'search/'+temp+'/';
			location.href = loc;
		}else{
			//this is where the category search steps in
			location.href = 'http://'+siteUrl+'/'+method+'/search/'+temp+'/';
		}
	}
	
}














/* BELOW ARE OLD SCRIPTS?!?! */
function changeCoverSrc(img, type){
	var oldSrc = img.src
	if(type == 'front'){
		var newSrc = oldSrc.replace("front", "back");
	}else{
		var newSrc = oldSrc.replace("back", "front");
	}
	img.src = newSrc; 
}

function fnShowDesc(desc){
	document.getElementById('desc').innerHTML = desc;
}

function msgBox(divke){
	var viewportwidth;
	var viewportheight;
	var msgheight;
 
 	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	if (typeof window.innerWidth != 'undefined'){
	  viewportwidth = window.innerWidth
	  viewportheight = window.innerHeight
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 	else if (
		typeof document.documentElement != 'undefined'
     	&& typeof document.documentElement.clientWidth !=
     	'undefined' && document.documentElement.clientWidth != 0
	){
       viewportwidth = document.documentElement.clientWidth
       viewportheight = document.documentElement.clientHeight
	}
 // older versions of IE
 
	else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}

	var msgbox = divke;
	var x = (viewportwidth / 2) - (parseInt(msgbox.style.width,10)) / 2;
	var y = (viewportheight / 2) - (parseInt(msgbox.style.height,10)) / 2;  
	msgbox.style.top = y+"px";
	msgbox.style.left = x+"px";

//divke.style.top = y+document.body.scrollTop;
}



//START BOOKMARK AND HOMEPAGE SCRIPT
var ua=navigator.userAgent.toLowerCase();
var ischrome=(ua.indexOf('chrome')!=-1);
var isKonq=(ua.indexOf('konqueror')!=-1);
var isSafari=(ua.indexOf('webkit')!=-1);
var isMac=(ua.indexOf('mac')!=-1);
var buttonStr=isMac?'Command/Cmd':'CTRL';
	

var bmTitle = "Dvddownload.com"; 
var bmUrl 	= "http://www.dvddownload.com/";	
	
	// START BOOKMARK SCRIPT
	function Bookmark() {
	
		if (window.sidebar){ // Mozilla Firefox Bookmark
			window.sidebar.addPanel(bmTitle, bmUrl,"");
		}else if( window.external ){ 
			if(ischrome){ //Chrome
				alert('U dient op CTRL + D te drukken om onze site aan uw favorieten toe te voegen.');
			}else{ // IE Favorite
				window.external.AddFavorite( bmUrl, bmTitle);
			}
		}else{
			if(isKonq){ //Chrome
				alert('U dient op CTRL + B te drukken om onze site aan uw favorieten toe te voegen.');
			}else if(window.home || isSafari){ // Firefox, Netscape, Safari, iCab
				alert('U dient op '+buttonStr+' + D te drukken om onze site aan uw favorieten toe te voegen.');
			}else if(!window.print || isMac){ // IE5/Mac and Safari 1.0
				alert('U dient op Command/Cmd + D te drukken om onze site aan uw favorieten toe te voegen.');    
			}else{
				alert('U dient '+bmTitle+' handmatig toe te voegen aan uw favorieten');
			}
		}
	}
	// END BOOKMARK SCRIPT
	// START HOMEPAGE SCRIPT
	function Homepage(){	
		if (window.sidebar){ // Mozilla Firefox Bookmark
			alert('Sleep het icoontje links naast het webadres naar het "Home" icoontje in de werkbalk, om '+bmTitle+' in te stellen als jouw startpagina.');
		}else if(window.external){ // IE Favorite
			if(ischrome){ //Chrome
				alert('Om '+bmTitle+' als uw startpagina in te stellen, dient u dit handmatig in te stellen in uw browser opties.');
			}else{
				theobj = document.anchors[0];
				theobj.style.behavior='url(#default#homepage)';
				theobj.setHomePage(bmUrl);
			}
		}else{
			alert('Om '+title+' als uw startpagina in te stellen, dient u dit handmatig in te stellen in uw browser opties.');
		}
	}
	// END HOMEPAGE SCRIPT
//END BOOKMARK AND HOMEPAGE SCRIPT
