$(document).ready(function(){
	
	var _href = $('base').attr('href');
	
	if( _href.lastIndexOf('/') <= 0 ){
		_href+='/';
	}
	
	var qs = $.jqURL.qs({ ret:'object' });
	
	/*LOCALE*/
	$.fn.getLocale = function(){ return ($.cookie('locale')==null)?'tr':$.cookie('locale');	};	
	$.fn.setLocale = function(locale){	$.cookie('locale',locale,{ path: '/' }); };	
	/*LOCALE*/
	
	if( $.cookie('locale')==null || 'tr' == $.cookie('locale') ){
		$('#lang_en').show();
	}else if( 'en' == $.cookie('locale') ){
		$('#lang_tr').show();
	}
	
	/*CHANGE LANGUAGE*/
	$.fn.changeLanguage = function(locale){
		$().setLocale( locale );
		var q = $.jqURL.qs();	
		q = q.replace(/&lang=(en|tr)/g,'');
		gotoPage( _href+'index.php?'+q+'&lang='+locale  );
	}
	/*CHANGE LANGUAGE*/
	
	/*RANDOM IMAGE*/
	var bgImageTotal=4;
	var randomNumber = Math.round(Math.random()*(bgImageTotal-1))+1;
	imgPath=(_href+'/images/top_2x1_'+randomNumber+'.png');
	$('td#randomImageLoc').css('background-image', ('url("'+imgPath+'")'));
	/*RANDOM IMAGE*/
	
	function gotoPage(  _url  )
	{
		$.jqURL.loc( _url );
	}
	
	
	
	
	var pParam = (typeof(qs.cv)!='undefined')?qs.cv:'';
	var idParam = (typeof(qs.id)!='undefined')?qs.id:'';
	var pidParam = (typeof(qs.pid)!='undefined')?qs.pid:'';
	
	if( pParam=='home' || pParam=='' ){
		
		$('#productsDiv').load(_href+'/subpages/news.php',{'token':$('#stoken').val()},function(status){
			$(this).slider({
				vertical:true
			});
		}).addClass('newsSlider');
		
		
		
		/*
		if( $('#slideNews').length ){
			$('#slideNews').slider({
				vertical:true
			});
		}
		*/		
		
	}else{
		var data = {};
		data.token = $('#stoken').val();
		data.id	   = idParam;
		if( typeof(pidParam)!='undefined' ){
			data.pid = 	pidParam		
		}
		$('#productsDiv').load(
			_href+'/subpages/products.php',
			data,
			function(){
				$('a[rev="lastnode"]').bind('click',function(){
					var rel = $(this).attr('rel');
					$().openProducts( rel );
				})
			}
		);
		
	}
	

	
	
	$('li.topMenu').bind('click',function(){
		var _id = $(this).attr('id');		
		var role = $(this).attr('role');
		_id=_id.replace(/q_/,'');
		$.cookie('uid',_id);	
		var _url = _href+'index.php?';
		if( typeof(role)!='undefined' && role!=''){
			_url+='cv='+role+'&';
		}else{
			_url+='cv=content&';
		}
		_url+='id='+_id;		
		gotoPage( _url );
	});
	
	$('a.topHref').bind('mouseover mouseout',function(event){
		
		
		if( 'mouseover' == event.type  )
		{
			$(this).parent('td').addClass('topMenuColOver');
		}else if( 'mouseout' == event.type  ){
			$(this).parent('td').removeClass('topMenuColOver');
		}
	
	});
	
	$('a.topHref').bind('click',function(){
		var _id = $(this).attr('id');		
		var role = $(this).attr('role');
		_id=_id.replace(/q_/,'');
		$.cookie('uid',_id);
		var _url = _href+'index.php?';
		if( typeof(role)!='undefined' && role!='' ){
			_url+='cv='+role+'&';
		}else{
			_url+='cv=content&';
		}
		_url+='id='+_id;		
		gotoPage( _url );
	});
	
	$('a.siteHref').bind('click',function(){		
		var _id = $(this).attr('id');		
		var role = $(this).attr('role');
		_id=_id.replace(/q_/,'');
		var _url = _href+'index.php?';
		if( typeof(role)!='undefined' ){
			_url+='cv='+role+'&';
		}else{
			_url+='cv=content&';
		}
		_url+='id='+_id;		
		gotoPage( _url );
	});
	
	/* */
	$.fn.openProducts = function( rel )	{
		var _url = _href+'index.php?';
		var dm = rel.split('|');		
		if( dm[0]!='' && typeof(dm)!='undefined' && dm!='null' ){
			_url+='cv='+encodeURI( dm[0] )+'&';
		}else{
			_url+='cv='+encodeURI( 'content' )+'&';
		}
		
		if( typeof( $.cookie('uid') ) != 'undefined' ){
			_url+='pid='+$.cookie('uid')+'&';
		}
		_url+='id='+dm[1];
		//console.log( _url );	
		gotoPage( _url );
	}
		
	/**
	*
	*/
	$.fn.showDialog = function showDialog( _title,_msg )
	{
		$('#dialog').attr({'title':_title});		
		$('#dialog').html( '<p>'+_msg+'</p>' );
		$('#dialog').dialog({
			position:['center','center'],
			resizable:false,
			modal:true,
			title:_title,
			buttons: { "Tamam": function() { 
					$('#dialog').html( '' );
					$(this).dialog("close"); 
				}}
		});
	}
	
		
	
	$.validator.addMethod('stext',function(value,element){
		var re = /[A-Za-z0-9ÇÖŞĞÜİıçöğüş]/;
		return re.test(value);
	},'Yalnızca [A-Za-z0-9ÇÖŞĞÜİıçöğüş]');
	
	var subValidate = $('#searchform').validate({
		  rules: {
    		'q': {
				required: true,
      			maxlength:255,
				stext:true
    			}
  			},			
			messages:{
				'q': {
					required: 'Zorunlu alan',
      				maxlength: 'En çok {0} karakter'
				}
			}
	});
	/*
	$('#searchform').ajaxForm({ 
		url: _href + 'index.php',
		type:'GET',
		dataType:  'json',
		data:{'q':encodeURI( $('#q').val() ),'token':$('#stoken').val()},
		timeout:   3000,
		cache:false,	
		success: function( xhr ){
			showDialog( xhr.title , xhr.message );
			$('#submail').val('');
		},
		error: function( e,k,g ){
			showDialog( "HATA" , "İşleminiz Gerçekleştirilemedi!");
		},
		beforeSubmit:function(formData, jqForm, options){
			return subValidate.form();
		}
	});
	*/
	
	$.fn.siteSearch  = function()
	{
		
		if( subValidate.form() ){			
			$('#searchform').submit();	
			return true;
		}
		return false;
	}
	
})
